package org.example.maven.utils;


import org.apache.poi.ss.usermodel.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;


public class ExcelUtils {
    private static final DataFormatter dataFormatter = new DataFormatter();


    public static String getCellStringValue(Cell cell) {
        if (cell == null) return "";
        return dataFormatter.formatCellValue(cell).trim();
    }
    public static LocalDateTime getCellDateValue(Cell cell) {
        if (cell == null) return null;

        try {
            // 验证是否为日期格式单元格
            if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
                Date javaDate = cell.getDateCellValue();

                // 将 java.util.Date 转换为 java.time.LocalDateTime
                return javaDate.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime();
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException("日期解析失败（行：" + (cell.getRow().getRowNum()+1) + "）");
        }
    }
}
